//先定义metronic的APP
var App = {
    getViewPort: function() {
        var e = window,
            a = 'inner';
        if (!('innerWidth' in window)) {
            a = 'client';
            e = document.documentElement || document.body;
        }

        return {
            width: e[a + 'Width'],
            height: e[a + 'Height']
        };
    },
    getResponsiveBreakpoint: function(size) {
        // bootstrap responsive breakpoints
        var sizes = {
            'xs': 480,     // extra small
            'sm': 768,     // small
            'md': 992,     // medium
            'lg': 1200     // large
        };

        return sizes[size] ? sizes[size] : 0;
    },
    scrollTo: function(el, offeset) {
        var pos = (el && el.size() > 0) ? el.offset().top : 0;

        if (el) {
            if ($('body').hasClass('page-header-fixed')) {
                pos = pos - $('.page-header').height();
            } else if ($('body').hasClass('page-header-top-fixed')) {
                pos = pos - $('.page-header-top').height();
            } else if ($('body').hasClass('page-header-menu-fixed')) {
                pos = pos - $('.page-header-menu').height();
            }
            pos = pos + (offeset ? offeset : -1 * el.height());
        }

        $('html,body').animate({
            scrollTop: pos
        }, 'slow');
    },
};

var resBreakpointMd = App.getResponsiveBreakpoint('md');
var handleSidebarAndContentHeight = function() {
    var content = $('.page-content');
    var sidebar = $('.page-sidebar');
    var body = $('body');
    var height;

    if (body.hasClass("page-footer-fixed") === true && body.hasClass("page-sidebar-fixed") === false) {
        var available_height = App.getViewPort().height - $('.page-footer').outerHeight() - $('.page-header').outerHeight();
        if (content.height() < available_height) {
            content.attr('style', 'min-height:' + available_height + 'px');
        }
    } else {
        if (body.hasClass('page-sidebar-fixed')) {
            height = _calculateFixedSidebarViewportHeight();
            if (body.hasClass('page-footer-fixed') === false) {
                height = height - $('.page-footer').outerHeight();
            }
        } else {
            var headerHeight = $('.page-header').outerHeight();
            var footerHeight = $('.page-footer').outerHeight();

            if (App.getViewPort().width < resBreakpointMd) {
                height = App.getViewPort().height - headerHeight - footerHeight;
            } else {
                height = sidebar.height() + 20;
            }

            if ((height + headerHeight + footerHeight) <= App.getViewPort().height) {
                height = App.getViewPort().height - headerHeight - footerHeight;
            }
        }
        content.attr('style', 'min-height:' + height + 'px');
    }
};

$(function() {
    var body = $('body');

    //quickbar点击事件
    $('#quick-btn').on('click', function(e) {
        body.toggleClass('page-quick-sidebar-open');
    });


    //处理sidebar菜单点击事件


    $('.page-sidebar-menu').on('click', 'li > a.nav-toggle, li > a > span.nav-toggle', function(e) {
        var that = $(this).closest('.nav-item').children('.nav-link');

        if (App.getViewPort().width >= resBreakpointMd && !$('.page-sidebar-menu').attr("data-initialized") && $('body').hasClass('page-sidebar-closed') && that.parent('li').parent('.page-sidebar-menu').size() === 1) {
            return;
        }

        var hasSubMenu = that.next().hasClass('sub-menu');

        if (App.getViewPort().width >= resBreakpointMd && that.parents('.page-sidebar-menu-hover-submenu').size() === 1) { // exit of hover sidebar menu
            return;
        }

        if (hasSubMenu === false) {
            if (App.getViewPort().width < resBreakpointMd && $('.page-sidebar').hasClass("in")) { // close the menu on mobile view while laoding a page 
                $('.page-header .responsive-toggler').click();
            }
            return;
        }

        var parent = that.parent().parent();
        var the = that;
        var menu = $('.page-sidebar-menu');
        var sub = that.next();

        var autoScroll = menu.data("auto-scroll");
        var slideSpeed = parseInt(menu.data("slide-speed"));
        var keepExpand = menu.data("keep-expanded");

        if (!keepExpand) {
            parent.children('li.open').children('a').children('.arrow').removeClass('open');
            parent.children('li.open').children('.sub-menu:not(.always-open)').slideUp(slideSpeed);
            parent.children('li.open').removeClass('open');
        }

        var slideOffeset = -200;

        if (sub.is(":visible")) {
            $('.arrow', the).removeClass("open");
            the.parent().removeClass("open");
            sub.slideUp(slideSpeed, function() {
                if (autoScroll === true && $('body').hasClass('page-sidebar-closed') === false) {
                    if ($('body').hasClass('page-sidebar-fixed')) {
                        menu.slimScroll({
                            'scrollTo': (the.position()).top
                        });
                    } else {
                        App.scrollTo(the, slideOffeset);
                    }
                }
                handleSidebarAndContentHeight();
            });
        } else if (hasSubMenu) {
            $('.arrow', the).addClass("open");
            the.parent().addClass("open");
            sub.slideDown(slideSpeed, function() {
                if (autoScroll === true && $('body').hasClass('page-sidebar-closed') === false) {
                    if ($('body').hasClass('page-sidebar-fixed')) {
                        menu.slimScroll({
                            'scrollTo': (the.position()).top
                        });
                    } else {
                        App.scrollTo(the, slideOffeset);
                    }
                }
                handleSidebarAndContentHeight();
            });
        }

        e.preventDefault();
    });
    //toggle sidebar尺寸
    $('body').on('click', '.sidebar-toggler', function(e) {
        var sidebar = $('.page-sidebar').not('.page-quick-sidebar');
        var sidebarMenu = sidebar.find('.page-sidebar-menu');
        $(".sidebar-search", sidebar).removeClass("open");

        if (body.hasClass("page-sidebar-closed")) {
            body.removeClass("page-sidebar-closed");
            sidebarMenu.removeClass("page-sidebar-menu-closed");
            if ($.cookie) {
                $.cookie('sidebar_closed', '0');
            }
        } else {
            body.addClass("page-sidebar-closed");
            sidebarMenu.addClass("page-sidebar-menu-closed");
            if (body.hasClass("page-sidebar-fixed")) {
                sidebarMenu.trigger("mouseleave");
            }
            if ($.cookie) {
                $.cookie('sidebar_closed', '1');
            }
        }

        $(window).trigger('resize');
    });
});